home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / wasm223.zip / STRING.ASM < prev    next >
Assembly Source File  |  1993-05-04  |  3KB  |  101 lines

  1. ;********************************;
  2. ; WASM String Module             ;
  3. ; By Eric Tauck                  ;
  4. ;                                ;
  5. ; Defines:                       ;
  6. ;                                ;
  7. ;   StrLen  return string length ;
  8. ;   StrCpy  copy string          ;
  9. ;   StrCmp  compare two strings  ;
  10. ;********************************;
  11.  
  12.         jmps    _string_end
  13.  
  14. ;========================================
  15. ; Determine the length of a string.
  16. ;
  17. ; In: AX= string address.
  18. ;
  19. ; Out: AX= length.
  20.  
  21. StrLen  PROC    NEAR
  22.         push    di
  23.         mov     cx, 0FFFFH      ;byte to scan
  24.         mov     di, ax          ;address
  25.         sub     al, al          ;byte to scan for
  26.         cld                     ;forward direction
  27.         repne
  28.         scasb                   ;scan for zero
  29.         mov     ax, cx          ;remaining bytes
  30.         inc     ax
  31.         inc     ax              ;adjust
  32.         neg     ax              ;byte count
  33.         pop     di
  34.         ret
  35.         ENDP
  36.  
  37. ;========================================
  38. ; Copy a string to a new address.
  39. ;
  40. ; In: AX= source address; BX=
  41. ;     destination address.
  42. ;
  43. ; Out: AX= length of string copied.
  44.  
  45. StrCpy  PROC    NEAR
  46.         push    di
  47.         push    si
  48.         mov     si, ax          ;load source
  49.         mov     di, bx          ;load destination
  50.         sub     ax, ax          ;zero length
  51.         cld                     ;forward direction
  52.         mov     ax, si          ;starting offset
  53. _stcpy1 movsb                   ;copy byte
  54.         cmp     BYTE [si-1], 0  ;check if end of string
  55.         jnz     _stcpy1         ;loop back if not
  56.         sub     ax, si          ;negative displacement
  57.         neg     ax              ;return length
  58.         dec     ax              ;don't count NUL
  59.         pop     si
  60.         pop     di
  61.         ret
  62.         ENDP
  63.  
  64. ;========================================
  65. ; Compare two strings.
  66. ;
  67. ; In: AX= string one; BX= string two.
  68. ;
  69. ; Out: CY= set if different.
  70.  
  71. StrCmp  PROC    NEAR
  72.         push    di
  73.         push    si
  74.         mov     si, ax          ;string one
  75.         mov     di, bx          ;string two
  76.         cld                     ;forward direction
  77.  
  78. ;--- loop for each byte
  79.  
  80. _stcmp1 cmpsb                   ;compare bytes
  81.         jne     _stcmp2         ;jump if not same
  82.         cmp     BYTE [si-1], 0  ;check if end of string
  83.         jnz     _stcmp1         ;loop back if not
  84.  
  85. ;--- matched
  86.  
  87.         pop     si
  88.         pop     di
  89.         clc
  90.         ret
  91.  
  92. ;--- no match
  93.  
  94. _stcmp2 pop     si
  95.         pop     di
  96.         stc
  97.         ret
  98.         ENDP
  99.  
  100. _string_end
  101.